# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=maeparser
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.3.2
pkgrel=1
pkgdesc="Maestro file parser. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/schrodinger/maeparser'
msys2_references=(
  'archlinux: maeparser'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost")
source=("https://github.com/schrodinger/maeparser/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('431ae029957534324b59eb3974486f3cad97d06e9bacd88ec94dc598046dfcd3')

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DMAEPARSER_BUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DMAEPARSER_BUILD_TESTS=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

check() {
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -Bbuild-${MSYSTEM} \
    -S${_realname}-${pkgver} \
    -DMAEPARSER_BUILD_TESTS=ON
  "${MINGW_PREFIX}"/bin/cmake.exe --build build-${MSYSTEM}

  PATH="${srcdir}"/build-${MSYSTEM}:$PATH \
    "${MINGW_PREFIX}"/bin/ctest --test-dir build-${MSYSTEM} || echo "Tests failed"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}"${MINGW_PREFIX}/lib/cmake/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
}
